package com.atguigu.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * TTL队列       配置文件类代码 P59
 */
@Configuration
public class TtlQueueConfig {

    //普通交换机的名称
    public static final String X_EXCHANGE = "X";
    //死信交换机的名称
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";

    //普通队列的名称
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    //死信队列的名称
    public static final String DEAD_LETTER_QUEUE_D = "QD";

    //新的普通队列
    public static final String QUEUE_C = "QC";

    //声明QC队列
    @Bean("queueC")
    public Queue queueC() {
        Map<String, Object> arguments = new HashMap<>(3);

        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key", "YD");

        //TTL
        return QueueBuilder
                .durable(QUEUE_C)
                .withArguments(arguments).build();

    }



    //声明普通交换机  别名
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    //声明死信交换机
    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange) {

        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }


    //声明普通队列 (包含过期时间 为10S)
    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信交换机的Routing的key
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置TTL   单位是ms
        arguments.put("x-message-ttl", 10000);

        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }


    //声明普通队列 (包含过期时间 为40S)
    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信交换机的Routing的key
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置TTL   单位是ms
        arguments.put("x-message-ttl", 40000);
        //给队列添加参数
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //死信队列
    @Bean("queueD")
    public Queue queueD() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_D).build();
    }

    /**
     * @Bean传入IOC中的bean,如果有大量重复类型的类
     * 使用@Qualifier("") 按名称id注入
     *
     * 绑定普通队列A和普通交换机
     * @param queueA
     * @return
     */
    @Bean
    public Binding queueABingX(@Qualifier("queueA") Queue queueA,
                               @Qualifier("xExchange") DirectExchange xExchange) {

        return BindingBuilder
                //绑定队列A
                .bind(queueA)
                //指定交换机
                .to(xExchange)
                //指定管理的routingKey
                .with("XA");
    }


    @Bean
    public Binding queueBBingX(@Qualifier("queueB") Queue queueB,
                               @Qualifier("xExchange") DirectExchange xExchange) {

        return BindingBuilder
                //绑定队列A
                .bind(queueB)
                //指定交换机
                .to(xExchange)
                //指定管理的routingKey
                .with("XB");
    }


    @Bean
    public Binding queueDBingY(@Qualifier("queueD") Queue queueD,
                               @Qualifier("yExchange") DirectExchange yExchange) {

        return BindingBuilder
                //绑定队列A
                .bind(queueD)
                //指定交换机
                .to(yExchange)
                //指定管理的routingKey
                .with("YD");
    }


}